<?php
namespace app\wadmin\controller;
// +----------------------------------------------------------------------
// | bbcThink
// +----------------------------------------------------------------------
// | Copyright (c) 2025 http://bbcweb.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: bbcweb <22745529@qq.com>
// +----------------------------------------------------------------------
use think\facade\Db;
use app\BaseController;
use think\facade\View;

//广告图
class Banner extends BaseController
{
    protected $table='banner';

    public function index()
    {
        $p=input();
        $where = array();
        $where[]=['category_id','=',1];
        if(!empty($p['keyword']))
        {
            $where[]=['title','like','%'.$p['keyword'].'%'];
        }
        $datalist = Db::name($this->table)->where($where)->order('sort asc,id desc')->paginate(20,false,['path'=>url('admin/banner/index')]);
        $list=array();
        foreach($datalist as &$v)
        {
            $v['category_name'] =Db::name('BannerCategory')->where(['id'=>$v['category_id']])->value('name','');
            $list[]=$v;
        }
        $page = $datalist->render();
        View::assign('data_list',$list);
        View::assign('page',$page);
        return View::fetch();
    }
    public function nav()
    {
        $p=input();
        $where = array();
        $where[]=['category_id','=',2];
        if(!empty($p['keyword']))
        {
            $where[]=['title','like','%'.$p['keyword'].'%'];
        }
        $datalist = Db::name($this->table)->where($where)->order('sort asc,id desc')->paginate(20,false,['path'=>url('admin/banner/index')]);
        $list=array();
        foreach($datalist as &$v)
        {
            $v['category_name'] =Db::name('BannerCategory')->where(['id'=>$v['category_id']])->value('name','');
            $list[]=$v;
        }
        $page = $datalist->render();
        View::assign('data_list',$list);
        View::assign('page',$page);
        return View::fetch();
    }
    public function modify()
    {
        $p=input();
        View::assign('category',Db::name('BannerCategory')->select()->toarray());
        $data=array();
        $category_id=1;
        if(!empty($p['category_id']))
        {
            $category_id=$p['category_id'];
        }
        
        if(isset($p['id']))
        {
            $data = Db::name($this->table)->where(['id'=>intval($p['id'])])->find();
            $category_id=$data['category_id'];
            $data['images_str']=$data['images'];
            $data['images']=handleImg($data['images']);
        }
        View::assign('category_id',$category_id);
        View::assign('data',$data);
        return View::fetch();
    }
    public function save()
    {
        $p=input();
        unset($p['file']);
        if(empty($p['status']))
        {
            $p['status'] = 0;
        }
        if(!empty($p['id']))
        {
            $id = $p['id'];
            unset($p['id']);            
            $res = Db::name($this->table)->where(['id'=>$id])->update($p);
        }
        else{
            $res = Db::name($this->table)->insert($p);
        }
        $category_id=$p['category_id'];
        if($res)
        {
            return jsonReturn(0,'操作成功',['category_id'=>$category_id]);
        }
        return jsonReturn(-1,'');
    }
    public function status()
    {
        $p = input();
        $res = Db::name($this->table)->where(['id'=>intval($p['id'])])->update(['status'=>intval($p['status'])]);
        return jsonRes($res);
    }
    public function remove()
    {
        $p = input();
        $res = Db::name($this->table)->where(['id'=>intval($p['id'])])->delete();
        return jsonRes($res);
    }
    public function removeall()
    {
        $p = input();
        $res = Db::name($this->table)->where([['id','in',$p['ids']]])->delete();
        return jsonRes($res);
    }
}